﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using NLog;

    public class CDZDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public bool Delete(object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_KeyCode), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public DataTable GetDataTable(string ps_Subsidiary, object pd_Ngay_Ct, object ps_Loai_DD)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_Loai_DD", ps_Loai_DD) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Sp_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDataTable(string ps_Subsidiary, object pd_Ngay_Ct, object ps_Loai_DD, object ps_Code)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_Loai_DD", ps_Loai_DD), new SqlParameter("p_Ma_Sp", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public int Save(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[12];
            values[0] = new SqlParameter("p_Moi_Sua", pb_NewChecked);
            values[1] = new SqlParameter("p_Stt", row["Stt"]);
            values[2] = new SqlParameter("p_Ma_DvCs", row["Ma_DvCs"]);
            values[3] = new SqlParameter("p_Ngay_Ct", row["Ngay_Ct"]);
            values[4] = new SqlParameter("p_Ma_Sp", row["Ma_Sp"]);
            values[5] = new SqlParameter("p_Ma_Yt", row["Ma_Yt"]);
            if (row.Table.Columns.Contains("Ma_Vt_Nh"))
            {
                values[6] = new SqlParameter("p_Ma_Vt_Nh", row["Ma_Vt_Nh"]);
            }
            else
            {
                values[6] = new SqlParameter("p_Ma_Vt_Nh", null);
            }
            if (row.Table.Columns.Contains("Ma_Dm_Sp"))
            {
                values[7] = new SqlParameter("p_Ma_Dm_Sp", row["Ma_Dm_Sp"]);
            }
            else
            {
                values[7] = new SqlParameter("p_Ma_Dm_Sp", null);
            }
            if (row.Table.Columns.Contains("Ton_Cuoi"))
            {
                values[8] = new SqlParameter("p_Ton_Cuoi", row["Ton_Cuoi"]);
            }
            else
            {
                values[8] = new SqlParameter("p_Ton_Cuoi", null);
            }
            values[9] = new SqlParameter("p_Du_Cuoi", row["Du_Cuoi"]);
            values[10] = new SqlParameter("p_Loai_DD", row["Loai_DD"]);
            values[11] = new SqlParameter("p_Result", SqlDbType.BigInt);
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[11].Direction = ParameterDirection.Output;
            values[11].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdZ_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[11].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

